<?php 

Class runModel extends appModel
{
	private $pageSize = 20;
	public function getApplyList($page=1)
	{
		$where = "";
// 		if(isset($user_name))
// 		{
// 			$where.=" name like '%".$user_name."%' AND ";
// 		}
		$where .= " 1=1  order by  p1.vote desc ";
		$listPage=array('data'=>'','pageLink'=>'');
		$result=$this->get_page(" p1.id,p1.name,p1.age,p1.telphone,p1.run_group,p1.reason,p1.pic_url,p2.openid,p2.nickname ",$this->table('run_apply')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid ",$where,$page,$this->pageSize);
		foreach ($result as $k=>$v)
		{
			$sql = "SELECT COUNT(*) FROM ".$this->table("run_vote")." WHERE rid = ".$v['id'];
			$result[$k]['count'] = $this->getOne($sql);
		}
		$listPage['pagelink']=$this->getpagelink();
		$listPage['data']=$result;
		$sql="SELECT count(*) FROM ".$this->table('run_apply')." p1 LEFT JOIN ".$this->table('wxusers')." p2 ON p1.wxid = p2.wxid WHERE ".$where;
		$count=$this->getOne($sql);
		$listPage['count']=$count;
		return $listPage;
	}
	
	public function getApplyAll()
	{
		$sql = "SELECT p1.id,p1.name,p1.age,p1.telphone,p1.run_group,p1.reason,p2.nickname,p2.openid,p1.vote FROM ".
				$this->table('run_apply')." p1 
					LEFT JOIN ".$this->table('wxusers')." p2 
							ON p1.wxid = p2.wxid order by  p1.vote desc";
		$result = $this->getAll($sql);
		$i = 0;
		foreach ($result as $k => $v)
		{
			$i++;
			$result[$k]['id'] = $i;
			$sql = "SELECT COUNT(*) FROM ".$this->table("run_vote")." WHERE rid = ".$v['id'];
			$result[$k]['vote'] = $this->getOne($sql);
		}
		return $result;
	}
	
}